package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.product.dao.GoodsDao;
import com.buss.product.model.Goods;

@Repository
public class GoodsDaoImpl implements GoodsDao {

	@Autowired
	private BaseDao<Goods> baseDao;
	
	@Override
	public Goods saveGoods(Goods goods) {
		return baseDao.saveOrEdit(goods);
	}

	@Override
	public void editGoods(Goods goods) {
		baseDao.edit(goods);
	}

	@Override
	public void removeGoods(Long[] ids) {
		String sql = "delete from Goods goods where goods.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public Goods loadGoods(Long id) {
		return baseDao.get(id, Goods.class);
	}

	@Override
	public Goods loadGoods(Long categoryId, String code) {
		String sql = "select goods from Goods goods where goods.categoryId = :categoryId " +
				"and goods.code = :code";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		params.put("code", code);
		return baseDao.loadHsql(sql, params);
	}

	public Goods loadGoods(String companyId,String code){
		String sql = "select goods from Goods goods where goods.category.companyId = :companyId " +
				"and goods.code = :code";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("code", code);
		return baseDao.loadHsql(sql, params);
	}
	
	@Override
	public Goods loadGoodsByStyleNo(Long categoryId, String styleNo) {
		String sql = "select goods from Goods goods where goods.categoryId = :categoryId " +
				"and goods.styleNo = :styleNo";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		params.put("styleNo", styleNo);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public ResultList<Goods> queryGoods(Long[] categoryIds, String name,
			String pinyin, Long brandId, String branchIds,String styleNo,
			RecordStatus status, OrderBy orderBy, Paginalbe page) {
		String sql = "select goods from Goods goods where goods.category.id in (:categoryIds) ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryIds", categoryIds);
		if(StringUtil.isNotEmpty(name)){
			sql += " and goods.name like :name ";
			params.put("name", "%"+name+"%");
		}
		if(StringUtil.isNotEmpty(pinyin)){
			sql += " and (goods.pinyin like :pinyin or goods.pinyinHead like :pinyin) ";
			params.put("pinyin", "%" + pinyin + "%");
		}
		if(brandId != null){
			sql += " and goods.brand.id = :brandId ";
			params.put("brandId", brandId);
		}
		if(StringUtil.isNotEmpty(branchIds)){
			if(branchIds.equals("0")){
				sql += " and (goods.branchId = '0') ";
			}else{
				sql += " and (goods.branchId = '0' or goods.branchId like :branchIds) ";
				params.put("branchIds", "," + branchIds + ",");
			}			
		}
		if(StringUtil.isNotEmpty(styleNo)){
			sql += " and goods.styleNo = :styleNo ";
			params.put("styleNo", styleNo);
		}
		if(status != null){
			sql += " and goods.status = :status ";
			params.put("status", status);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.DESC);
		}
		if(orderBy.getSidx().equals("categoryName")){
			orderBy.setSidx("name");
			sql += orderBy.aggOrder("goods.category");
		}else if(orderBy.getSidx().equals("brandName")){
			orderBy.setSidx("name");
			sql += orderBy.aggOrder("goods.brand");
		}else{
			sql += orderBy.aggOrder("goods");
		}
		return baseDao.queryHsql(sql, params, page);
	}

}
